﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Diagnostics;
using System.Windows.Controls.Primitives;

namespace Xceed.Silverlight.DataGrid
{
  public partial class DataGridControl
  {
    private class ScrollUpdateRequest : UpdateRequest
    {
      private ScrollUpdateRequest( ScrollEventType eventType, bool useTransition )
        :base()
      {
        this.EventType = eventType;

        this.ShouldInitialize = ( this.EventType == ScrollEventType.SmallDecrement
                              || this.EventType == ScrollEventType.SmallIncrement );

        if( useTransition )
        {
          this.UpdateDetails.TransitionHint = ScrollUpdateRequest.GetTransitionForEventType( eventType );
        }
      }

      public ScrollUpdateRequest( bool useTransition, ScrollEventType eventType )
        : this( eventType, useTransition )
      {
        Debug.Assert( eventType.IsDecrement() || eventType.IsIncrement() || eventType.IsFirst() || eventType.IsLast() );
        this.UpdateDetails.RowPreload = eventType.IsLargeChange();
      }

      public ScrollUpdateRequest( bool useTransition, double skipCount )
        : this( ScrollEventType.ThumbTrack, useTransition )
      {
        this.Count = skipCount;
      }

      public bool IsLargeChange
      {
        get
        {
          return ( this.EventType == ScrollEventType.LargeIncrement
            || this.EventType == ScrollEventType.LargeDecrement );
        }
      }

      private static TransitionHint GetTransitionForEventType( ScrollEventType eventType )
      {
        switch( eventType )
        {
          case ScrollEventType.ThumbTrack:
            {
              return TransitionHint.Update;
            }
          case ScrollEventType.First:
          case ScrollEventType.SmallDecrement:
          case ScrollEventType.LargeDecrement:
            {
              return TransitionHint.ScrollBackward;
            }
          case ScrollEventType.Last:
          case ScrollEventType.SmallIncrement:
          case ScrollEventType.LargeIncrement:
            {
              return TransitionHint.ScrollForward;
            }
          default:
            throw new DataGridInternalException();
        }
      }

      public ScrollEventType EventType;
      public double? Count;
    }
  }
}
